<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="fr" dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
	<title>gestion du cabinet d'ost&eacute;opathie</title>
	<meta name="author" content="Quentin Cartier" />
	<meta http-equiv="content-language" content="FR" />
	<link href="style.css?time=<?php echo(filemtime("style.css")); ?>" rel="stylesheet" type="text/css" />
	<link rel="shortcut icon" type="image/x-icon" href="images/favicon.ico">
	<script type="text/javascript">
		function catchevent(e) {
/* 			if (window.event) myevent = window.event;
			else myevent = e;
 */
			ConfirmCancel("", true, e);
/*
			var el = e.target ? e.target : e.srcElement;
			var event = event || window.event;
				if (event.preventDefault) {
					event.preventDefault();
				}
				event.returnValue = false;
			}
*/		}
	</script>
</head>
<?php
	include "php.php";
	CheckAuth();
	CheckSession();

	$linkDB=OpenDB();

	if($_SERVER['REQUEST_METHOD'] == "POST") { // validation d'une form
		if (isset($_POST['action']) && ($_POST['action'] == "update")) {
			if ($_POST['venueid']==0) { // création d'une nouvelle venue
				$query="INSERT INTO venues(id, idutilisateur, idpatient, motifvenue, traitement, datevenue, datecreation, datemodif) VALUES (";
				$query.="NULL, ".$_SESSION['userid'].", ".$_SESSION['patid'].", ".MysqlReady($_POST['motifvenue']).", ".MysqlReady($_POST['traitement']).", '".FormatDate($_POST['datevenue'])."', NOW(), NOW())";
				$_SESSION['message']="cr&eacute;ation r&eacute;alis&eacute;e avec succ&egrave;s";
			} else { // mise à jour d'une venue
				$query="UPDATE venues SET motifvenue=".MysqlReady($_POST['motifvenue']).", traitement=".MysqlReady($_POST['traitement']).", datevenue='".FormatDate($_POST['datevenue'])."', datemodif=NOW()";
				$query.=" WHERE id=".$_POST['venueid'];
				$_SESSION['message']="mise &agrave; jour r&eacute;alis&eacute;e avec succ&egrave;s";
			}
			$result=mysql_query ($query)
				or die ("impossible d\'ex&eacute;cuter la requ&ecirc;te : '".$query."'\n<br />".mysql_error()."\n");
//			echo $query;
			if ($_POST['venueid']==0) {
				$_SESSION['venueid'] = mysql_insert_id();
			}
		} else { // view
			$_SESSION['venueid']=$_POST['venueid'];
			if (isset($_POST['action']) && ($_POST['action'] == "search")) { // résultat de la recherche
				$_SESSION['patid']=$_POST['patid'];
				$_SESSION['patnom']=$_POST['patnom'];
				$_SESSION['patprenom']=$_POST['patprenom'];
			}
		}
	}
	$venueid = $_SESSION['venueid'];
?>
<body onbeforeunload="catchevent(event);">

	<script type="text/javascript" src="js/scripts.js"></script>
	<script type="text/javascript">
		function Properties(id) {
			if (ConfirmCancel("")) {
				document.forms['prop'].elements['venueid'].value=id;
				onbeforeunload=null;
				document.forms['prop'].submit();
			}
		}

		function CheckAndValidate() {
			var dt=document.forms['prop'].elements['motifvenue'];
			if(dt.value == "") {
				alert("le motif de venue ne peut &#234;tre vide");
				dt.focus();
				return;
			}
			var dt=document.forms['prop'].elements['datevenue']
			if (dt.value != "" && isDate(dt.value)==false){
				dt.focus();
				return;
			}
			document.forms['prop'].elements['action'].value="update";
			onbeforeunload=null;
			document.forms['prop'].submit();
		}
		
		function disableEnterKey(e) {
			 var key;     
			 if(window.event)
				  key = window.event.keyCode; //IE
			 else
				  key = e.which; //firefox     

			 return (key != 13);
		}

		var ConfirmCancelFormName="prop";

		function DateModified(e) {
			myinput = e.target ? e.target : e.srcElement;
			if(!isDate(myinput.value)) {
				document.getElementById('age').innerHTML="";
				return;
			}
			var d1 = DateFromString(patientddn);
			var d2 = DateFromString(myinput.value);

			var lage=age(d1,d2);
			if (lage!="") document.getElementById('age').innerHTML="&nbsp;age : " + lage;
			else document.getElementById('age').innerHTML="";
		}
		</script>
<?php // Les messages
	AlertDiv();
?>
	<!-- pour le calendrier -->
	<table class="ds_box" cellpadding="0" cellspacing="0" id="ds_conclass" style="display:none;">
		<tr>
			<td id="ds_calclass">
			</td>
		</tr>
	</table>
	<script type="text/javascript" src="js/calendar.js"></script>
	<table width="100%">
		<tr>
			<td>
				<div class="fixedH50">
					<table width="100%">
						<tr>
							<td class="cc1">
								&nbsp;
							</td>
							<td class="cc2">
								&nbsp;
							</td>
							<td class="cc3">
								&nbsp;
							</td>
							<td class="cc4">
								&nbsp;
							</td>
							<td class="cc5">
								&nbsp;
							</td>
							<td align="right" nowrap>
								<div class="titre"><?php echo $_SESSION['user']; ?>&nbsp;</div>
							</td>
							<td align="right" width="1%" nowrap>
								<a href="venue.php" onclick="Properties(0); return false;" title="nouvelle venue">
									<img src="images/newvenue48.png" />
								</a>
								<a href="historique.php" onclick="ConfirmCancel('historique.php'); return false;" title="historique des venues">
									<img src="images/venue48.png" />
								</a>
								<a href="" onclick="ShowPrint(); return false;" title="impressions">
									<img src="images/print48.png" />
								</a>
								<a href="listepatients.php" onclick="ConfirmCancel('listepatients.php'); return false;" title="liste des patients">
									<img src="images/userlist48.png" />
								</a>
								<a href="index.php" onclick="ConfirmCancel('index.php'); return false;" title="retour &agrave; l'accueil">
									<img src="images/home48.png" />
								</a>
								<a href="logout.php" onclick="ConfirmCancel('logout.php'); return false;" title="sortir">
									<img src="images/exit48.png" />
								</a>
							</td>
						</tr>
					</table>
				</div>
			</td>
		</tr>
		<tr>
			<td align="center">
				<form method="post" action="venue.php" name="prop">
					<input type="hidden" name="action" value="view" />
					<input type="hidden" name="venueid" value="<?php echo $venueid; ?>" />
					<table cellpadding="1" cellspacing="1">
						<tr>
							<td class="c1" align="center" colspan="2" nowrap="nowrap">
<?php
	if ($venueid != 0) {
		//recup de la venue
		$query="SELECT T1.id, T1.idutilisateur, T2.alias, T1.motifvenue, T1.traitement, DATE_FORMAT(T1.datevenue, '%d/%m/%Y'), T1.datevenue";
		$query.=" FROM venues as T1, utilisateurs as T2";
		$query.=" WHERE T1.id=".$venueid;
		$query.=" AND T1.idutilisateur = T2.id";
		$result=mysql_query ($query)
			or die ("impossible d\'ex&eacute;cuter la requ&ecirc;te : '".$query."'\n<br />".mysql_error()."\n");
		// echo $query;

		$rownum=0;
		while($line=mysql_fetch_row($result)){
			$i=0;
			$rownum++;
			foreach ($line as $col_value){
				$data[$i++]=$col_value;
			}

			if ($i == 0) {
			} else {
				$j=0;
				$venueid=$data[$j++];
				$venueidutilisateur=$data[$j++];
				$aliasutilisateur=$data[$j++];
				$venuemotif=EncodeAccents($data[$j++]);
				$venuetraitement=EncodeAccents($data[$j++]);
				$venuedate=$data[$j++];
				$venuedatefull=$data[$j++];
			}
		}
	echo "
								&nbsp;<b>venue de <i>".$_SESSION['patnom']." ".$_SESSION['patprenom']."</i></b>&nbsp;";
	} else {
		$venueid=$venueidutilisateur=0;
		$venuemotif=$venuetraitement=$venuedate=$venuedatefull="";
		echo "
								&nbsp;<b>nouvelle venue de <i>".$_SESSION['patnom']." ".$_SESSION['patprenom']."</i></b>&nbsp;";
	}

	$classRO="";
	$isRO=false;
	if (($venueidutilisateur != $_SESSION['userid'] && $_SESSION['droitadmin'] == 0) && $venueid != 0) {
		$classRO="readonly=\"readonly\"";
		$isRO=true;
		echo "g&eacute;r&eacute; par ".$aliasutilisateur;
	}
?>
							</td>
						</tr>
						<tr>
							<td class="c4" align="left" nowrap="nowrap">
								date
							</td>
							<td class="c4" align="left">
								<input type="text" name="datevenue" value="<?php if($venueid==0) echo date("d/m/Y"); else echo $venuedate; ?>" size="10" onKeyPress="return disableEnterKey(event)" onkeyup="Date_handleKeyup(event);" <?php echo $classRO; ?>>
<?php
	if (!$isRO) echo "
								<a href=\"\" onclick=\"javascript:ds_sh(document.forms['prop'].elements['datevenue']);return false\" title=\"choisir une date\">
									<img src=\"images/schedule16.png\" />
								</a>";
?>
								<div id="age" class="inline"></div>
							</td>
						</tr>
						<tr>
							<td class="c4" align="left" nowrap="nowrap">
								motif de venue
							</td>
							<td class="c4">
								<textarea name="motifvenue" id="motifvenue" rows="5" cols="60" onkeyup="textAreaAdjust(this)" <?php echo $classRO; ?>><?php echo $venuemotif; ?></textarea>
							</td>
						</tr>
						<tr>
							<td class="c4" align="left" nowrap="nowrap">
								traitement
							</td>
							<td class="c4">
								<textarea name="traitement" rows="10" cols="60" onkeyup="textAreaAdjust(this)" <?php echo $classRO; ?>><?php echo $venuetraitement; ?></textarea>
							</td>
						</tr>

<?php 
	//recup des venues précédantes et suivante
	$query="(SELECT id, \"suiv\" FROM venues WHERE idpatient=".$_SESSION['patid']." AND datevenue >= '".$venuedatefull."' AND id!=".$venueid." ORDER BY datevenue ASC LIMIT 0, 1)";
	$query.=" UNION";
	$query.=" (SELECT id, \"prec\" FROM venues WHERE idpatient=".$_SESSION['patid']." AND datevenue < '".$venuedatefull."' AND id!=".$venueid." ORDER BY datevenue DESC LIMIT 0, 1)";
	$result=mysql_query ($query)
		or die ("impossible d\'ex&eacute;cuter la requ&ecirc;te : '".$query."'\n<br />".mysql_error()."\n");
	// echo $query;

	$rownum=0;
	while($line=mysql_fetch_row($result)) {
		$i=0;
		foreach ($line as $col_value){
			$data[$i++]=$col_value;
		}
		$venuehist[$data[1]]=$data[0];
		$rownum++;
	}
?>
						<tr>
							<td class="c3" align="center" colspan="2">
<?php
	if (isset($venuehist['prec'])) {
		echo "
								<a href=\"venue.php\" onclick=\"Properties(".$venuehist['prec']."); return false;\" title=\"venue pr&eacute;c&eacute;dante\">
									<img src=\"images/arrowleft48.png\" />
								</a>";
	} else {
		echo "
									<img src=\"images/arrowleftgrey48.png\" />";
	 }
	 
	 echo "
								<a href=\"patient.php\" onclick=\"ConfirmCancel('patient.php');return false;\" title=\"retour au patient\">
									<img src=\"images/back48.png\" />
								</a>";

	if (!$isRO) {
		echo "
								<a href=\"patient.php\" onclick=\"CheckAndValidate();return false;\" title=\"valider\">
									<img src=\"images/ok48.png\" />
								</a>";
	}
	if (isset($venuehist['suiv'])) {
		echo "
								<a href=\"venue.php\" onclick=\"Properties(".$venuehist['suiv']."); return false;\" title=\"venue suivante\">
									<img src=\"images/arrowright48.png\" />
								</a>";
	} else {
		echo "
								<img src=\"images/arrowrightgrey48.png\" />";
	}
?>
							</td>
						</tr>		
					</table>
				</form>
			</td>
		</tr>
	</table>
<?php
	// Gestion des mots clefs
	include("keywords.php");

	// Page d'options d'impression
	include "printopt.php";	

	CloseDB($linkDB);
?>
	<script type="text/javascript">
		// ajout du support de l'age
		var patientddn="<?php echo $_SESSION['datenaiss']; ?>";

		if (document.addEventListener) {
			document.forms['prop'].elements['datevenue'].addEventListener("blur", DateModified, false);
		}
		else if (document.attachEvent) {
			document.forms['prop'].elements['datevenue'].attachEvent("onblur", DateModified, false);
		}
		if (document.forms['prop'].elements['datevenue'].value!="00/00/0000")
			document.forms['prop'].elements['datevenue'].focus();
		document.getElementById('motifvenue').focus();
	</script>
		<div class="note">cr&eacute;ation: Quentin Cartier 2012</div>
</body>
</html>